package game;

import globals.GlobalObjects;
import globals.GlobalParameters;
import globals.ObjectFactory;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

import physics.Avatar;
import physics.Knife;
import physics.SatelliteBox;

import common.Math3D;
import common.Position3D;


public class Game extends java.awt.event.KeyAdapter {

	Universe universe=null;
	Avatar avatar;
	Boolean isPaused=false;
	SatelliteBox satCamera = new SatelliteBox();
	ObjectFactory objFactory = new ObjectFactory();
	
	static int level = 1;
	
	private static int deadRobots = 0;
	private static int robotsToKill = 10;
	static boolean avatarWon = false;
	private static boolean ended = false;
	
	Timer timer;
	int repaintFrequence;


	public Game(){
		universe = new Universe();
	}


	KeyAdapter keyAdapter = new KeyAdapter() {

		boolean satelliteCameraOn = false;
		double distStep = 0.5;
		double camDistanceDiff = 0;
		TracingCamera tc;

		@Override
		public void keyPressed(KeyEvent k) {
			tc = (TracingCamera)universe.getCamera();

			switch (k.keyCode) {
			case 'p':
				pause();
				break;
			}

			if (!satelliteCameraOn){     //regular Mode
				//UP		    	
				if (k.keyCode == 16777217)
					avatar.stepForward();
				//DOWN
				if (k.keyCode == 16777218)
					avatar.stepBackward();
				//LEFT
				if (k.keyCode == 16777219)
					avatar.rotateRight(-0.05);

				//RIGHT
				if (k.keyCode == 16777220)
					avatar.rotateRight(0.05);

				if (k.keyCode == 32)
					avatar.jump();

				if (k.keyCode == 65536){
					synchronized (GlobalObjects.synchronizer) {
						avatar.fire();
					}
				}
				//tab
				if (k.keyCode == 9){
						avatar.changeWeapon();
				}
				
				if (k.keyCode == 109){
					satCamera.setPosition(calculateSatCameraPosition());
					satCamera.setPlanet(avatar.getPlanet());
					avatar.getPlanet().addObjectToPlanet(satCamera);

					tc.setTarget(avatar.getPlanet().getPosition(),satCamera.getPosition());
					tc.setDistance(GlobalParameters.satcamDistance);
					satelliteCameraOn = true;
				}

			} else{ //Map Mode

				if (k.keyCode == 109){
					tc.setTarget(avatar.getPosition());
					tc.setDistance(GlobalParameters.avatarDistance);
					satelliteCameraOn = false;
					camDistanceDiff = 0;
				}
				//UP		    	
				if (k.keyCode == 16777217)
					satCamera.goForward();

				//DOWN
				if (k.keyCode == 16777218)
					satCamera.goBackward();

				//LEFT
				if (k.keyCode == 16777219)
					satCamera.goLeft();

				//RIGHT
				if (k.keyCode == 16777220)
					satCamera.goRight();
				//satCamera.rotateRight(0.1);

				//PAGEUP
				if (k.keyCode == 16777221)
					satCamera.rotateLeft();

				//PAGEDOWN
				if (k.keyCode == 16777222)
					satCamera.rotateRight();

				// [
				if (k.keyCode == 91)
					satCamera.rotateLeft();

				// ]
				if (k.keyCode == 93)
					satCamera.rotateRight();

				//Up (+) Zoom in
				if (k.keyCode == 61){
					double distance = tc.getUpDistance();
					if (distance > 1){
						tc.setUpDistance(distance - distStep);
					}
				}

				//Down(-) Zoom out
				if (k.keyCode == 45){		    				  	
					tc.setUpDistance(tc.getUpDistance() + distStep);
				}


			}
		}
	};
	




	static public boolean canEnterPortal(Avatar avatar) {
		boolean levelIsDone = false;
		switch(level){
			case 1:
				if (deadRobots >= robotsToKill)
					levelIsDone = true;
					break;
			case 2:
				levelIsDone = true;
				
			case 3:
				if (deadRobots >= robotsToKill+1)
					levelIsDone = true;
			}

		return levelIsDone;
	}


	public void start( int rf )
	{
		ended = false;
		repaintFrequence = rf;
	}


	public void pause()
	{
		if (!isPaused){
			isPaused=true;
		}else{
			isPaused=false;
		}
	}

	public void setAvatar(Avatar box) {
		this.avatar = box;
		Universe.setAvatar(this.avatar);
	}

	public Avatar getAvatar() {
		return avatar;
	}

	public Universe getUniverse() {
		return universe;
	}

	public void setRepaintFrequence(int repaintFrequence) {
		this.repaintFrequence = repaintFrequence;
	}

	public KeyAdapter getKeyAdapter() {
		return keyAdapter;
	}

	public SatelliteBox getSatCamera() {
		return satCamera;
	}

	public void setSatCamera(SatelliteBox sCamera) {
		this.satCamera = sCamera;
	}

	public void updSatCamera(Position3D pos) {
		this.satCamera.setPosition(pos);
	}


	private Position3D calculateSatCameraPosition()
	{
		Position3D camPos = new Position3D();
		camPos.copy(avatar.getPlanet().getPosition());
		double height = avatar.getPlanet().getRadius()*2;
		camPos.getCenter().add(Math3D.scalePoint(avatar.getUp(), height));
		return camPos;
	}

	public ObjectFactory getObjFactory() {
		return objFactory;
	}


	public static void doneLevel() {
		level++;
	}

	public void actionPerformed(ActionEvent event) {
		if (!isPaused)
			Universe.actionPerformed(event);
	}
	
	public static boolean ended() {
		return ended;
	}

	public boolean avatarWon() {		
		return avatarWon;
	}
	
	public String getCurrentMission(){
		String line = " ";
		switch(level){
		case 1:
			if (deadRobots < robotsToKill){
				line +=  (robotsToKill - deadRobots) + " robbots to kill";
			}else{
				line += "Mission accomplished! Go to the portal.";
			}
			break;
		case 2:
			line += "Go to the portal. Be careful.";
			break;
		
		case 3:
			if (deadRobots < robotsToKill+1){
				line += "Kill the bastard";
			}else{
				line += "Mission accomplished! Go to the portal.";
			}
			
			break;
		
		case 4:
			line += "Find the Carrot. The rest you must figure out by yourself";
			break;	
			
		}
		return line;
	}
	
	public static void  robotDead(){
		deadRobots++;
	}
	
	public static void avatarDead() {
		ended = true;
		avatarWon = false;
	}
	
	public static void  gameOver(){
		ended = true;
		avatarWon = true;
	}
	
}